package cn.wolfcode.wolf2w.job;

import cn.wolfcode.wolf2w.redis.test.RedisByKey;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.util.StrategyStatisVO;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

//@Component
public class RedisDataPersistanceJob {

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Scheduled(cron = "0/5 * * * * ?")
    public void dowork(){
        //从redis中去获取所有的vo对象
        //1、先拼接出一个规则的key
        String key = RedisByKey.STRATEGY_STATIC_VO.join("*");
        //2、然后得到所有vo对象的key值
        Set<String> keys = redisTemplate.keys(key);
        List<StrategyStatisVO> vos=new ArrayList<>();
        //便利获取vo对象的集合
        if(keys!=null&&keys.size()>0){
            for(String key1:keys){
                String s = redisTemplate.opsForValue().get(key1);
                StrategyStatisVO vo = JSON.parseObject(s, StrategyStatisVO.class);
                vos.add(vo);
            }
        }
        //便利vo对象执行攻略统计数据更新
        for(StrategyStatisVO vo:vos){
            strategyService.updateStrategyVo(vo);
        }
    }
}
